<?php

class ProductsController extends AppController {

    var $name = 'Products';
    var $paginate = array('limit' => 5, 'order' => array('Product.name' => 'asc'));

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('search', 'details');
    }

    function index() {
        $this->Product->recursive = 0;
        $this->set('products', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid product', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('product', $this->Product->read(null, $id));
    }

    function details($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid product', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('product', $this->Product->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Product->create();
            if ($this->Product->save($this->data)) {
                $this->Session->setFlash(__('Η αποθηκεύση έγινε επιτυχώς.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        $brands = $this->Product->Brand->find('list');
        $categories = $this->Product->Category->find('list');
//        $orders = $this->Product->Order->find('list');
        $this->set(compact('brands', 'categories'/*, 'orders'*/));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid product', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Product->save($this->data)) {
                $this->Session->setFlash(__('Η αποθηκεύση έγινε επιτυχώς.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Product->read(null, $id);
        }
        $brands = $this->Product->Brand->find('list');
        $categories = $this->Product->Category->find('list');
//        $orders = $this->Product->Order->find('list');
        $this->set(compact('brands', 'categories'/*, 'orders'*/));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for product', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Product->delete($id)) {
            $this->Session->setFlash(__('Η διαγραφή έγινε επιτυχώς.', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Η διαγραφή απέτυχε.', true));
        $this->redirect(array('action' => 'index'));
    }

    function search($category_id = 0, $brand_id = 0) {
        if ($category_id == 0 && $brand_id == 0) {
            $conditions = array();
        } else if ($category_id == 0) {
            $conditions = array('Product.brand_id' => $brand_id);
        } else if ($brand_id == 0) {
            $conditions = array('Product.category_id' => $category_id);
        } else {
            $conditions = array('Product.category_id' => $category_id, 'Product.brand_id' => $brand_id);
        }
//        if (empty($conditions)) {
//            $result = $this->Product->find('all');
//        }else{
//            $result = $this->Product->find('all',array('conditions' => $conditions));
//        }
//        $this->set('result', $result);
        if (empty($conditions)) {
        $this->set('result', $this->paginate('Product'));
        }else{
            $this->set('result', $this->paginate('Product', $conditions));
        }
    }

}
